# Maintainer: Ray Donnelly <mingw.android@gmail.com>
# Maintainer: Martell Malone <martellmalone@gmail.com>

_realname=ag
_longname=the_silver_searcher
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=2.1.0.r1975.d83e205
pkgrel=1
pkgdesc="The Silver Searcher: An attempt to make something better than ack, which itself is better than grep (mingw-w64)"
arch=('any')
url="https://geoff.greer.fm/ag"
license=("Apache")
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc"
             "${MINGW_PACKAGE_PREFIX}-pkg-config"
             "git")
depends=("${MINGW_PACKAGE_PREFIX}-pcre"
         "${MINGW_PACKAGE_PREFIX}-xz"
         "${MINGW_PACKAGE_PREFIX}-zlib")
options=('staticlibs') # '!strip' 'debug')
source=("${_realname}"::"git+https://github.com/ggreer/the_silver_searcher.git"
        "0001-lang-Allow-both-extensions-and-full-filenames.patch"
        "0002-win32-Detect-Cygwin-MSYS-PTY.patch"
        "0003-options-Fix-ordering-problems-with-color.patch"
        "0004-lang-Add-autotools-ac-am-in-m4-pc.patch"
        "0005-lang-Add-to-make-Makefile-Makefile.Debug-Makefile.Re.patch"
        "0006-lang-Add-makepkg-PKGBUILD-diff-patch-in-install.patch"
        "0007-lang-Add-cmake-CMakeLists.txt-CMakeCache.txt.patch")
sha256sums=('SKIP'
            '9a3c1cc11547219f699e953e05c53b779f396b3d54d1410ae78a62f7550ca7c7'
            '45b75b164cb283c4324e27a46c744e0f2df86e4860506e9a4f2c1e98e848f51e'
            '63ded2c4bf859faa8dc33cbda89c4bbdb047b014d7b3b4d3f77f9dfb5d1a0ee4'
            '3a42e02928f7ae288de69f91c198c6111216f0af23d2de1bc65d8f5e5a28a401'
            'fada103d58911e876af36e76675793764c4019e009351d615dede9c3ba7618d1'
            'b80e3efd854425e0e5df29cdf585b5132b5ab19e93c06713a4f4f0653bed5f84'
            '0659935f2f5e6c38260a603ef696de7e3e5d8dd7f62ec7711b58bd657ab5b511')

pkgver() {
  cd ${srcdir}/${_realname}
  local AC_INIT_VER=$(grep AC_INIT configure.ac -A5 | tr '\n' ' ' | sed -e 's/AC_INIT([^,]\+,\s*\[\?\([0-9.a-z]\+\).*/\1/')
  printf "%s.r%s.%s" "${AC_INIT_VER}" "$(git rev-list --count HEAD)" "$(git rev-parse --short HEAD)"
}

prepare() {
  cd "${srcdir}"/${_realname}
  git am --committer-date-is-author-date "${srcdir}"/0001-lang-Allow-both-extensions-and-full-filenames.patch
  git am --committer-date-is-author-date "${srcdir}"/0002-win32-Detect-Cygwin-MSYS-PTY.patch
  git am --committer-date-is-author-date "${srcdir}"/0003-options-Fix-ordering-problems-with-color.patch
  git am --committer-date-is-author-date "${srcdir}"/0004-lang-Add-autotools-ac-am-in-m4-pc.patch
  git am --committer-date-is-author-date "${srcdir}"/0005-lang-Add-to-make-Makefile-Makefile.Debug-Makefile.Re.patch
  git am --committer-date-is-author-date "${srcdir}"/0006-lang-Add-makepkg-PKGBUILD-diff-patch-in-install.patch
  git am --committer-date-is-author-date "${srcdir}"/0007-lang-Add-cmake-CMakeLists.txt-CMakeCache.txt.patch

  # configure.ac forces -O2, so force it to -O0 if debugging.
  if check_option "debug" "y"; then
    sed -i "s#-O2#-O0#g" configure.ac
  fi

  autoreconf -fi
}

build() {

  mkdir -p "${srcdir}/build-${CARCH}"
  cd "${srcdir}/build-${CARCH}"

  "${srcdir}"/${_realname}/configure \
    --prefix=${MINGW_PREFIX} \
    --{build,host}=${MINGW_CHOST}

  make
}

package() {
  cd "${srcdir}/build-${CARCH}"
  make DESTDIR="${pkgdir}" install
}
